import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

import '../common/flatPanel.dart';
import '../models/changeLogItem.dart';
import '../utils/screw.dart';

class ChangeLogDialog extends StatefulWidget {
  ChangeLogDialog({Key key, this.item}) : super(key: key);

  final ChangeLogItem item;

  @override
  _ChangeLogDialogState createState() => _ChangeLogDialogState();
}

class _ChangeLogDialogState extends State<ChangeLogDialog> {
  final _entry = TextEditingController();
  final _date = TextEditingController();
  final _version = TextEditingController();
  final _detail = TextEditingController();
  final GlobalKey _formKey = new GlobalKey<FormState>();

  void _onSubmit() {
    var item = ChangeLogItem(
      id: 0,
      entry: _entry.text,
      date: _date.text,
      version: _version.text,
      detail: _detail.text,
    );
    Navigator.pop(context, item);
  }

  @override
  void initState() {
    super.initState();
    if (widget.item == null) return;

    if (widget.item.id != null && widget.item.id != 0) {
      _entry.text = widget.item.entry;
      _date.text = widget.item.date;
      _version.text = widget.item.version;
      _detail.text = widget.item.detail;
    } else {
      if (widget.item.entry != null && widget.item.entry.isNotEmpty)
        _entry.text = widget.item.entry;
    }
  }

  Future<void> _openDatePicker() async {
    var result = await showDatePicker(
        context: context,
        initialDate: DateTime.now(),
        firstDate: DateTime(2020),
        lastDate: DateTime(2021));
    _date.text = Screw.getDateString(result);
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      width: MediaQuery.of(context).size.width * 0.5,
      height: MediaQuery.of(context).size.height * 0.6,
      child: FlatPanel(
        child: Form(
          key: _formKey,
          child: ListView(
            children: <Widget>[
              TextFormField(
                controller: _entry,
                decoration: InputDecoration(
                  hintText: '组件名',
                  prefixIcon: Icon(Icons.title),
                ),
                validator: (v) => v.trim().length > 0 ? null : '组件名不能为空',
              ),
              TextFormField(
                controller: _version,
                decoration: InputDecoration(
                    hintText: '版本号', prefixIcon: Icon(Icons.label_important)),
                validator: (v) => v.trim().length > 0 ? null : '版本号不能为空！',
              ),
              TextFormField(
                controller: _detail,
                decoration: InputDecoration(
                    hintText: '版本说明', prefixIcon: Icon(Icons.tag)),
                validator: (v) => v.trim().length > 0 ? null : '说明不能为空！',
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: TextFormField(
                      controller: _date,
                      decoration: InputDecoration(
                          labelText: '日期',
                          hintText: '更新日期',
                          prefixIcon: Icon(Icons.date_range)),
                      keyboardType: TextInputType.datetime,
                      validator: (v) => v.trim().length > 0 ? null : '日期不能为空！',
                    ),
                  ),
                  IconButton(
                      icon: Icon(Icons.calendar_today),
                      onPressed: _openDatePicker)
                ],
              ),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Expanded(
                    flex: 1,
                    child: FlatButton(
                        child: Text('清空', style: TextStyle(fontSize: 24)),
                        onPressed: () {
                          (_formKey.currentState as FormState).reset();
                        }),
                  ),
                  Expanded(
                    flex: 1,
                    child: FlatButton(
                      child: Text(widget.item.id == 0 ? '提交' : '保存',
                          style: TextStyle(fontSize: 24, color: Colors.blue)),
                      onPressed: () {
                        if ((_formKey.currentState as FormState).validate())
                          _onSubmit();
                      },
                    ),
                  ),
                ],
              )
            ],
          ),
        ),
      ),
    );
  }
}

Future<ChangeLogItem> showChangeLogDialog(BuildContext context,
    {ChangeLogItem item}) async {
  return await showDialog<ChangeLogItem>(
      context: context,
      builder: (context) {
        return Dialog(child: ChangeLogDialog(item: item));
      });
}
